**
** process output from Google Earth Engine
**

// Get the path of the currently running do-file
local do_file_path `c(pwd)'

// Change the working directory to the location of the do-file
cd "`do_file_path'"

** merge area
clear
import delimited "local\ee_data\burned_area\lake_chad_ntl_ctr_x_d01_ee_area.csv", varnames(1) clear
keep ee_area_km2 objectid 
rename ee_area_km2 area_ee
label variable area_ee "Area (km2) : EE"
save "local\ee_data\burned_area\lake_chad_ntl_ctr_x_d01_ee_area.dta", replace


clear
import delimited "local\ee_data\ndvi\lake_chad_gridid_mean_NDVI_mod13_ts2001.csv", varnames(1) clear 
* shape_area shape_leng x y geo id v98
drop system v14
foreach v of varlist v2-v13 {
   local x : variable label `v'
   local newname = substr("`x'", 1, 6)
   recode `v' (-999=.)
   rename `v' ndvi`newname'
}
drop geo
save "proc_data\NDVI_mean_ts2001.dta", replace


clear
import delimited "local\ee_data\ndvi\lake_chad_gridid_mean_NDVI_mod13_ts2002.csv", varnames(1) clear 
* shape_area shape_leng x y geo id v98
drop system v14
foreach v of varlist v2-v13 {
   local x : variable label `v'
   local newname = substr("`x'", 1, 6)
   recode `v' (-999=.)
   rename `v' ndvi`newname'
}
drop geo
save "proc_data\NDVI_mean_ts2002.dta", replace


clear
import delimited "local\ee_data\ndvi\lake_chad_gridid_mean_NDVI_mod13_ts2003.csv", varnames(1) clear 
* shape_area shape_leng x y geo id v98
drop system v14
foreach v of varlist v2-v13 {
   local x : variable label `v'
   local newname = substr("`x'", 1, 6)
   recode `v' (-999=.)
   rename `v' ndvi`newname'
}
drop geo
save "proc_data\NDVI_mean_ts2003.dta", replace

clear
import delimited "local\ee_data\ndvi\lake_chad_gridid_mean_NDVI_mod13_ts2004.csv", varnames(1) clear 
* shape_area shape_leng x y geo id v98
drop system v14
foreach v of varlist v2-v13 {
   local x : variable label `v'
   local newname = substr("`x'", 1, 6)
   recode `v' (-999=.)
   rename `v' ndvi`newname'
}
drop geo
save "proc_data\NDVI_mean_ts2004.dta", replace


clear
import delimited "local\ee_data\ndvi\lake_chad_gridid_mean_NDVI_mod13_ts2005_2009.csv", varnames(1) clear 
* shape_area shape_leng x y geo id v98
drop system v62
drop v50-v61
foreach v of varlist v2-v49 {
   local x : variable label `v'
   local newname = substr("`x'", 1, 6)
   recode `v' (-999=.)
   rename `v' ndvi`newname'
}
drop geo
save "proc_data\NDVI_mean_ts2005_2009.dta", replace


clear
import delimited "local\ee_data\ndvi\lake_chad_gridid_mean_NDVI_mod13_ts2009_2013.csv", varnames(1) clear 
* shape_area shape_leng x y geo id v98
drop system v62 
foreach v of varlist v2-v61 {
   local x : variable label `v'
   local newname = substr("`x'", 1, 6)
   recode `v' (-999=.)
   rename `v' ndvi`newname'
}
drop geo
save "proc_data\NDVI_mean_ts2009_2013.dta", replace


import delimited "local\ee_data\ndvi\lake_chad_gridid_mean_NDVI_mod13_ts2014_2018.csv", varnames(1) clear 
*shape_area shape_leng x y geo id v86
drop system geo v62
foreach v of varlist v2-v61 {
   local x : variable label `v'
   local newname = substr("`x'", 1, 6)
   recode `v' (-999=.)
   rename `v' ndvi`newname'
}
save "proc_data\NDVI_mean_ts2014_2018.dta", replace

merge 1:1 objectid gid_0 using "proc_data\NDVI_mean_ts2009_2013.dta"
drop _merge

* many objectid year
merge m:1 objectid using "proc_data\NDVI_mean_ts2005_2009.dta"
drop _merge

merge m:1 objectid using "proc_data\NDVI_mean_ts2004.dta"
drop _merge

merge m:1 objectid using "proc_data\NDVI_mean_ts2003.dta"
drop _merge

merge m:1 objectid using "proc_data\NDVI_mean_ts2002.dta"
drop _merge

merge m:1 objectid using "proc_data\NDVI_mean_ts2001.dta"
drop _merge


** panel
reshape long ndvi, i(objectid) j(yearmo) 

replace ndvi=0 if ndvi==.
g year = int(yearmo/100)
g month = mod(yearmo,100)

*keep if year >= 2001 & year <= 2015
sum year
*drop yearmo
*drop v98

* do not include country identifiers! 
*collapse (sum) ndvi area_ee, by(objectid yearmo year month gid_0)

* scale factor of 10000
replace ndvi = ndvi / 10000

label variable objectid "Seq id per grid or split grid at country border"
*label variable gid_0 "Country code from admin shp : GADM v3.6"
label variable ndvi "Mean NDVI (greenness) over 1 month : MOD EE"
drop yearmo

drop name_0

**
** create xt var
**
gen mdate = ym(year, month) 
format mdate %tm 
sort mdate 

sort objectid mdate

xtset objectid mdate
tsfill, full

replace year = yofd(dofm(mdate))
replace month = month(dofm(mdate))


save "proc_data\NDVI_mo_ts.dta", replace

sum